package com.sofa.shark.admin.api.fegin.impl;

import com.alipay.sofa.rpc.common.RpcConstants;
import com.alipay.sofa.rpc.config.ConsumerConfig;
import com.alipay.sofa.rpc.hystrix.HystrixConstants;
import com.alipay.sofa.rpc.hystrix.SofaHystrixConfig;
import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;
import com.alipay.sofa.runtime.api.annotation.SofaService;
import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding;
import com.sofa.shark.admin.api.entity.SysLog;
import com.sofa.shark.admin.api.fegin.RemoteLogService;
import com.sofa.shark.admin.api.fegin.SysLogService;
import com.sofa.shark.admin.api.fegin.factory.RemoteLogServiceFallbackFactory;
import com.sofa.shark.common.core.utils.R;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.stereotype.Component;

/**
 * @author tonyp
 */
@SofaService(interfaceType = RemoteLogService.class, bindings = {@SofaServiceBinding(bindingType = RpcConstants.PROTOCOL_TYPE_BOLT)})
@Component
@ConditionalOnWebApplication
public class RemoteLogServiceImpl implements RemoteLogService {

    @SofaReference(interfaceType = SysLogService.class, binding = @SofaReferenceBinding(bindingType = RpcConstants.PROTOCOL_TYPE_BOLT))
    private SysLogService sysLogService;

    public RemoteLogServiceImpl() {
        ConsumerConfig<RemoteLogService> consumerConfig = new ConsumerConfig<RemoteLogService>()
                .setInterfaceId(RemoteLogService.class.getName())
                .setParameter(HystrixConstants.SOFA_HYSTRIX_ENABLED, String.valueOf(true));
        // 可以自定义 FallbackFactory 直接注入 FallbackFactory
        SofaHystrixConfig.registerFallbackFactory(consumerConfig, new RemoteLogServiceFallbackFactory());
    }

    @Override
    public R<Boolean> saveLog(SysLog sysLog) {
        return R.ok(sysLogService.save(sysLog));
    }
}
